package org.batyrov.equery.action.admin;

import org.batyrov.equery.action.BaseAction;
import org.batyrov.equery.action.Const;
import org.batyrov.equery.bean.Service;
import org.batyrov.equery.bean.Window;

import javax.swing.text.html.parser.Entity;
import javax.persistence.EntityTransaction;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: root
 * Date: 25.12.2009
 * Time: 23:23:23
 * To change this template use File | Settings | File Templates.
 */
public class LinkWindowService extends BaseAction{
    private long windowId = -1;
    private long serviceId = -1;
    private String serviceName;
    private String windowName;

    public long getWindowId() {
        return windowId;
    }

    public void setWindowId(long windowId) {
        this.windowId = windowId;
    }

    public long getServiceId() {
        return serviceId;
    }

    public void setServiceId(long serviceId) {
        this.serviceId = serviceId;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getWindowName() {
        return windowName;
    }

    public void setWindowName(String windowName) {
        this.windowName = windowName;
    }

    @Override
    public String execute() throws Exception {
        super.execute();
        Service service =  em.find(Service.class, serviceId);
        if (service == null){
            addActionError(getText("cannot_found_service"));
            return Const.ERROR;
        }
        Window window = em.find(Window.class, windowId);
        if (window == null){
            addActionError(getText("cannot_found_window"));
            return Const.ERROR;
        }
        EntityTransaction tx = em.getTransaction();
        try{
            tx.begin();
            service.getWindows().add(window);
            tx.commit();
        }catch (Exception e){
            if (tx.isActive()){
                tx.rollback();
            }
        }
        return Const.SUCCESS;
    }
}
